<!-- loading加载动画通用组件（两种小球动态交互） -->

<script setup lang="ts">
</script>

<template>
<div class="loading">
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
</div>
</template>

<style scoped lang="scss">
$ballSize: 10px; // 小球尺寸
$containerSize: 150px; // 容器尺寸
$n: 36; // 小球的数量
$pdeg: calc(360deg / $n); // 小球的间隔角度
$ani-duration: 2000ms; // 动画时间



.loading {
  width: $containerSize;
  height: $containerSize;
  margin: 50px auto;
  position: relative;
  border-radius: 50%;

  .dot {
    position: absolute;
    left: 50%;
    top: 50%;
    width: $ballSize;
    height: $ballSize;
    margin-left: calc(-1 * #{$ballSize} / 2);
    margin-top: calc(-1 * #{$ballSize} / 2);
    // 通过 perspective 属性设置景深
    perspective: 70px;
    // 开启 3D 效果
    transform-style: preserve-3d;

    &::before,
    &::after {
      content: "";
      position: absolute;
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
    &::before {
      background: #f0f9eb;
      top: -100%;
      animation: moveBlack $ani-duration infinite;
    }
    &::after {
      background: #000000;
      top: 100%;
      animation: moveWhite $ani-duration infinite;
    }
  }
}

@keyframes moveBlack {
  0% {
    animation-timing-function: ease-in;
  }
  25% {
    transform: translate3d(0, 100%, $ballSize);
    animation-timing-function: ease-out;
  }
  50% {
    transform: translate3d(0, 200%, 0);
    animation-timing-function: ease-in;
  }
  75% {
    transform: translate3d(0, 100%, -$ballSize);
    animation-timing-function: ease-out;
  }
}


@keyframes moveWhite {
  0% {
    animation-timing-function: ease-in;
  }
  25% {
    transform: translate3d(0, -100%, -$ballSize);
    animation-timing-function: ease-out;
  }
  50% {
    transform: translate3d(0, -200%, 0);
    animation-timing-function: ease-in;
  }
  75% {
    transform: translate3d(0, -100%, $ballSize);
    animation-timing-function: ease-out;
  }
}

@for $i from 1 through  $n {
  .dot:nth-child(#{$i}) {
    transform: rotate(#{$i * $pdeg}) translateY(calc(-1 * #{ $containerSize } / 2));
    &::before,
    &::after {
      animation-delay: calc(-1 * #{$ani-duration} / #{$n} * 6 * #{$i});
    }
  }
}

</style>
